<?php
namespace Api\Model
{
    /**
     * Database access, directly using PDO feature
     * @author Lukasz Torba
     */
    class   DB2 extends \Api\Model
    {
        /**
         * Function initalize PDO 
         * Called by parent  
         */
        public function __initModel($config)
        {
            // TODO: Model initalize   
            if( ! array_key_exists('Database', $config))
            {
                throw new \Exception('DB2: Database confing doesn\'t exists');
            }
            
            	$dbconf = $config['Database'];		

			if(!(array_key_exists('method',$dbconf) && array_key_exists('name',$dbconf) &&
			array_key_exists('pass',$dbconf) && array_key_exists('user',$dbconf)))
				throw new \Exception("DB2: Invalid confing file");
									
			$this->m_handle = new \PDO($dbconf['method'].':host='.$dbconf['host'].';dbname='.$dbconf['name'],$dbconf['user'],$dbconf['pass'], array_key_exists('addition',$dbconf)?$dbconf['addition']:array());	
				
				
			return $config;
                        
        }
        
        
        public function __get($var)
        {
            // TODO: return pdo handler
            if($var == 'db') return $this->m_handle;
            return null;
        }
    }
}
?>